/**
 * Pawn.
 * 
 * @author Brian Boadi, Antoine Domenger
 * @version 1.0
 */

package lp.chess;

public class Pawn extends Piece {
	//- ATTRIBUTE(S) --------------------------------------------------------------------------
	
	/**
	 * True if this pawn has already been moved during the game.
	 */
	private boolean m_moved;
	
	//-----------------------------------------------------------------------------------------
	
	//- CONSTRUCTOR(S) ------------------------------------------------------------------------

	/**
 	 * Default constructor.
 	 * Initializes an empty pawn.
 	 */
	public Pawn() {
		super();
		m_moved = false;
	}

	/**
	 * Initializes a new pawn of a given color.
	 * 
	 * @param white true if the pawn must be white, false if it must be black.
	 */
	public Pawn(boolean white) {
		super(white);
		m_moved = false;
	}
	
	/**
	 * Initializes a new pawn with all of its attributes.
	 * 
	 * @param x	the x coordinate of the pawn on the chessboard.
	 * @param y the y coordinate of the pawn on the chessboard.
	 * @param white true if the pawn must be white, false if it must be black.
	 */
	public Pawn(int x, int y, boolean white) {
		super(x, y, white);
		m_moved = false;
	}
	
	/**
	 * Copy constructor.
	 * Initializes a new pawn from a given one.
	 * 
	 * @param other the pawn to copy in the new one.
	 */
	public Pawn(Pawn other) {
		super(other);
		m_moved = false;
	}
	
	//-----------------------------------------------------------------------------------------

	//- OTHER METHOD(S) -----------------------------------------------------------------------

	/**
	 * Checks if a move is valid for this kind of piece.
	 * 
	 * @param cb the chessboard.
	 * @param x	the x coordinate where we want the pawn to move on the chessboard.
	 * @param y the y coordinate where we want the pawn to move on the chessboard.
	 * @param act true if we want to remove the piece that can be taken.
	 * 
	 * @return true if the move is valid.
	 */
	public boolean validMove(Chessboard cb, int x, int y, boolean act)
	{
		boolean isValid = (cb.isOutOfRange(x, y) && differentPosition(x, y));
		
		// Specific to this piece :
		if(isValid)
		{
			// If it's the first move, the pawn can advance two cases:
			
			int nbCases;
			
			if(!m_moved) {
				if(act) m_moved = true;
				nbCases = 2;
			}
			else nbCases = 1;
			
			if(x != getX()) {	// The pawn tries to move horizontally.
				if(m_moved && cb.getPiece(x, y) == null) isValid = false;
				else {
					if(isWhite()) {	// The pawn is white.
						if(!(y == getY() - 1 && (x == getX() + 1 || x == getX() - 1))) isValid = false;
						else {	// The movement is valid.
							if(cb.getPiece(x, y).isWhite() != isWhite() && act) cb.removePiece(x, y);	// The piece can be taken.
						}
					}
					else {	// The pawn is black.
						if(!(y == getY() + 1 && (x == getX() + 1 || x == getX() - 1))) isValid = false;
						else {	// The movement is valid.
							if(cb.getPiece(x, y).isWhite() != isWhite() && act) cb.removePiece(x, y);	// The piece can be taken.
						}
					}
				}
			}
			else {	// The pawn goes straight forward the enemy.
				if(isWhite()) {	// The pawn is white.
					if(y < getY() - nbCases || y > getY()) isValid = false;
				}
				else {	// The pawn is black.
					if(y > getY() + nbCases || y < getY()) isValid = false;
				}
			}
		}
		
		if(isValid && cb.getPiece(x, y) != null) isValid = false;	// There is already a piece on this square.
		
		return isValid;
	}

	/**
	 * Returns the character associated to the type of this piece.
	 * 
	 * @return the character associated to the type of this piece 
	 */
	public char getType()
	{
		if(isWhite()) return 'P';
		else return 'p';
	}
	
	/**
	 * Returns a copy of this piece.
	 * 
	 * @return a copy of this piece.
	 */
	public Piece getCopy() {
		Pawn copy = new Pawn(m_x, m_y, m_white);
		
		return copy;
	}
	
	//-----------------------------------------------------------------------------------------
}
